package com.sx.pipeline.ui.wallet.withdraw

import android.text.TextUtils
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.custom.dialog.DialogChooseBankCard
import com.sx.pipeline.custom.dialog.DialogPasswordInput
import com.sx.pipeline.databinding.ActWithdrawBinding


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 提现
 */
@Route(path = RoutePath.Wallet.WITHDRAW)
class WithdrawActivity : CYDataBindingBaseActivity<ActWithdrawBinding,WithdrawVM>(R.layout.act_withdraw), View.OnClickListener {
    private var bankId=0 //银行卡id
    override fun initData() {
        mBinding.setVariable(BR.onClick, this)
        //提现手续费比例
        mBinding.tvCharge.text="提现手续费${mViewModel.mModel.getUserData()?.withdrawByRates}%"
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        mBinding.titleBar.rightView.setOnClickListener {
            //提现明细
            ARouter.getInstance().build(RoutePath.Wallet.WITHDRAW_DETAILS).navigation()
        }

    }

    override fun onResume() {
        super.onResume()
        //获取钱包余额
        mViewModel.updateUserInfo()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        observe(mViewModel.userInfoDataEvent){
            //余额
            mBinding.tvBalance.text=it?.currencyBalance
        }

        //银行卡列表
        observe(mViewModel.bankListEvent){
            val popup =
                DialogChooseBankCard(this, mViewModel.bankStringData)
            popup.setCommonPickerListener { index, data ->
               bankId= mViewModel.bankListEvent.value?.records?.get(index)?.id!!
                mBinding.tvBank.text=data
            }
            XPopup.Builder(this)
                .borderRadius(42f)
                .isDarkTheme(false)
                .isDestroyOnDismiss(false)
                .asCustom(popup)
                .show()
        }

        //提现结果
        observe(mViewModel.withdrawSuccessEvent){
            if (it==true){
                XPopup.Builder(this)
                    .isDestroyOnDismiss(true)
                    .asConfirm(getString(R.string.txt_commit_success), "请耐心等待审核",
                        getString(R.string.txt_back_home), getString(R.string.txt_okey),
                        {
                            finish()
                        }, { }, false
                    ).show()
            }
        }
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            //选择银行卡
            R.id.tab_bank -> {
                mViewModel.getBankCardList(mPage)
            }
            R.id.tv_all->{
                //全部提现
                mBinding.etMoney.setText(mBinding.tvBalance.text.toString())
            }
            //提现
            R.id.tv_withdraw -> {
                if (bankId==0) {
                    ToastUtils.showShort("请选择提现银行卡")
                    return
                }
                if (TextUtils.isEmpty(mBinding.etMoney.text.toString())) {
                    ToastUtils.showShort("请输入提现金额")
                    return
                }
                val popup =
                    DialogPasswordInput(this,"").setPassInputListener { payword ->
                        mViewModel.getWithdraw(bankId,payword,mBinding.etMoney.text.toString())
                    }
                XPopup.Builder(this)
                    .borderRadius(42f)
                    .isDarkTheme(false)
                    .asCustom(popup)
                    .show()
            }
        }

    }

}